<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 19-7-11
 * Time: 上午9:36
 */

namespace app\admin\controller;

use app\common\model\MenuModel;
use app\common\model\MenuRoleModel;
use app\common\model\RoleModel;
use think\Db;

class MenuController extends AdminBase
{
    public function lists()
    {
        if ($this->request->isAjax()) {
            $data = Db::field(['m.menu_id', 'm.menu_name', 'm.menu_pid', 'm.menu_sort', 'GROUP_CONCAT(r.role_name)' => 'role_name'])
                ->table('menu m')
                ->leftJoin('menu_role mr', 'm.menu_id = mr.menu_id')
                ->leftJoin('role r', 'mr.role_id = r.role_id')
                ->group('m.menu_id')
                ->order('menu_sort')
                ->all();
            $lists = self::combineMenu($data);
            $total = count($lists);
            return $this->listJson($lists, $total);
        } else {
            return view();
        }
    }

    public function add()
    {
        if($this->request->isAjax()){
            $menuData = $this->request->param();
            if(empty($menuData['menu_name'])) return $this->errJson('缺少参数');
            //用户名是否重复
            $menu = MenuModel::where('menu_name','=',$menuData['menu_name'])->find();
            if(isset($menu)) return $this->errJson('名称已存在,请重新输入');

            $res = MenuModel::create($menuData);

            if(!empty($menuData['roleId'])) {
                $roleIds = explode(',', $menuData['roleId']);
                $data = [];
                foreach ($roleIds as $key => $value){
                    $data[] = ['menu_id' => $res->menu_id, 'role_id' => $value];
                }
                $menuRole = new MenuRoleModel();
                $menuRole->saveAll($data, false);
            }

            if(isset($res)){
                return $this->okJson('添加成功');
            }else{
                return $this->errJson('添加失败');
            }

        } else {
            $menu = MenuModel::all()->toArray();
            $lists = self::combineMenu($menu);
            $this->assign('lists', $lists);
            $role = RoleModel::all();
            $this->assign('roles', $role);
            return view();
        }
    }

    public function edit()
    {
        $id = input('menu_id');
        if($this->request->isAjax()){
            $param = $this->request->param();
            unset($param['menu_id']);
            if(empty($param['menu_name']) || empty($id)) return $this->errJson('缺少参数');
            $menu = MenuModel::where('menu_id', $id)->find();

            if($menu['menu_name'] != $param['menu_name']){
                $menus = MenuModel::where('menu_name', $param['menu_name'])->find();
                if(!empty($menus)) return $this->errJson('名称已存在');
            }
            $res = MenuModel::update($param, ['menu_id' => $id]);

            $menuRole = new MenuRoleModel();
            $menuRole->where('menu_id', $id)->delete();
            if(!empty($param['roleId'])) {
                $roleId = trim($param['roleId'], ',');
                $roleIds = explode(',', $roleId);
                $data = [];
                foreach ($roleIds as $key => $value) {
                    $data[] = ['menu_id' => $id, 'role_id' => $value];
                }
                $menuRole->saveAll($data, false);
            }
            if($res) {
                return $this->okJson('ok');
            } else {
                return $this->errJson('error');
            }

        } else {
            $menu = MenuModel::get($id);
            $menus = MenuModel::all()->toArray();
            $menus = self::combineMenu($menus);
            $role = RoleModel::select();
            $myRoles = MenuRoleModel::where('menu_id', $id)->field('role_id')->select()->toArray();
            $myRoles = array_column($myRoles, 'role_id');
            $this->assign('menu',$menu);
            $this->assign('roles', $role);
            $this->assign('menus', $menus);
            $this->assign('myRoles', $myRoles);
            return view();
        }
    }

    public function del()
    {
        $id = input('id');
        if(empty($id)) return $this->errJson('id不能为空');
        MenuModel::destroy($id);
        return $this->okJson('ok');
    }

    public function editSort()
    {
        $id = input('id');
        $value = input('value');
        $sort = MenuModel::update(['menu_sort' => $value], ['menu_id' => $id]);
        if ($sort) {
            return $this->okJson('修改成功');
        } else {
            return $this->errJson('修改失败');
        }
    }
}